<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="projectName">
        <el-input
          v-model="queryParams.projectName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['project:project:add']"
        >添加</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-row>
      <el-col :span="7" v-for="project in projectList" :key="project.projectId" style="margin: 10px">
<!--        el-card 是 Element UI 组件库中提供的一个卡片组件，主要用于展示块级区域，如模块、卡片等。-->
        <el-card :body-style="{ padding: '0px', height: '550px' }">
<!--          图片-->
          <image-preview :src="project.projectImage" width="100%" height="40%" class="image"/>

<!--          div里具体信息显示-->
          <div style="padding: 14px;">
            <div class="bottom clearfix">
<!--              哪个项目-->
              <el-row>
                <el-col :span="12">
                  <div style="margin-bottom: 10px;">{{ project.projectName }}</div>
                </el-col>
                <el-col :span="12">
                  <div style="margin-bottom: 10px;">
                    <image-preview :src="project.projectLogo" class="image" style="width: 40px; height: 40px ;border-radius: 50%" />
                  </div>
                </el-col>
              </el-row>
<!--              计划进度：-->
              <div style="margin-bottom: 10px;">
                <span>
                  计划进度：
                </span>
<!--                el-progress 是 Element UI 中的一个进度条组件，用于显示任务的完成度-->
                <el-progress
                  style="display: inline-block; width: 75%;"
                  :text-inside="true"
                  :stroke-width="15"
                  :percentage="project.projectPlan"
                  status="success"
                  text-color="white"
                ></el-progress>
              </div>
<!--              实际进度：-->
              <div style="margin-bottom: 10px;">
                <span>
                  实际进度：
                </span>
                <el-progress
                  style="display: inline-block; width: 75%;"
                  :text-inside="true"
                  :stroke-width="15"
                  :percentage="project.projectActual"
                  status="success"
                  color="blue"
                  text-color="white"
                ></el-progress>
              </div>
<!--              已付费用，目标费用-->
              <el-tooltip class="item" effect="dark" :content="formatCurrency(project.projectPaidCost)" placement="top-end">
                <div style="margin-bottom: 10px;">
                  已付费用：￥{{ project.projectPaidCost | numberFormat}}
                </div>
              </el-tooltip>
              <el-tooltip class="item" effect="dark" :content="formatCurrency(project.projectTargetCost)" placement="top-end">
                <div style="margin-bottom: 10px;">
                  目标费用：￥{{project.projectTargetCost | numberFormat}}
                </div>
              </el-tooltip>
              <div style="margin-bottom: 10px;">
                竣工时间：{{ project.projectCompletetime }}
              </div>
              <div style="margin-bottom: 10px;">
                开业时间：{{ project.projectOpeningtime }}
              </div>
              <div style="margin-bottom: 10px;">
                <el-button type="text"  @click="handleRisk(project.projectId)">风险</el-button>
<!--                el-badge 是 Element UI 中的一个组件，用于在其他元素上显示一个徽章，通常用于显示未读消息数量、更新提醒等。徽章可以显示数字或者图标，常用于导航栏、头像旁边等场景。-->
                <el-badge :value="project.riskCount !=0 ? project.riskCount :null" class="item"></el-badge>
                <span style="padding-left: 20px">
                  <el-button
                    size="mini"
                    type="primary"
                    icon="el-icon-edit"
                    @click="handleUpdate(project.projectId)"
                    v-hasPermi="['project:project:edit']"
                  >修改</el-button>
                  <el-button
                    size="mini"
                    type="danger"
                    icon="el-icon-delete"
                    @click="handleDelete(project.projectId)"
                    v-hasPermi="['project:project:remove']"
                  >删除</el-button>
                </span>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :page-sizes="[12,24,48,96]"
      @pagination="getList"
    />
<!--    风险信息  dialog-->
    <el-dialog title="风险信息" width="60%" append-to-body :visible.sync="RiskOpen">
<!--      <span>这是查询的风险信息</span>-->
      <el-table v-loading="loading" :data="projectRiskList">
        <el-table-column label="序号" align="center" prop="riskId" />
        <el-table-column label="风险类别" align="center" prop="risktypeName" />
        <el-table-column label="风险标题" align="center" prop="riskTitle" />
        <el-table-column label="风险内容" align="center" prop="riskContent" />
        <el-table-column label="跟进人" align="center" prop="nickName" />
        <el-table-column label="状态" align="center" prop="riskStatus">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.riskStatus === 1" size="small" type="danger">待跟进</el-tag>
            <el-tag v-else-if="scope.row.riskStatus === 2" size="small" type="primary">跟进中</el-tag>
            <el-tag v-else-if="scope.row.riskStatus === 3" size="small" type="success">已解决</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <!-- 添加或修改项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="项目名称" prop="projectName">
              <el-input v-model="form.projectName" placeholder="请输入项目名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="开始时间" prop="startDate">
              <el-date-picker clearable
                              v-model="form.startDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="计划进度" prop="projectPlan">
              <el-input v-model="form.projectPlan" placeholder="请输入计划进度"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="实际进度" prop="projectActual">
              <el-input v-model="form.projectActual" placeholder="请输入实际进度"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row >
          <el-col :span="12">
            <el-form-item label="竣工时间" prop="projectCompletetime">
              <el-date-picker clearable
                              v-model="form.projectCompletetime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择竣工时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="开业时间" prop="projectOpeningtime">
              <el-date-picker clearable
                              v-model="form.projectOpeningtime"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择开业时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="目标成本" prop="projectTargetCost">
          <el-input-number v-model="form.projectTargetCost" placeholder="请输入目标成本" />
          <span style="margin-left: 10px">{{ formatCurrency(this.form.projectTargetCost)}}</span>
        </el-form-item>
        <el-form-item label="已付成本" prop="projectPaidCost">
          <el-input-number v-model="form.projectPaidCost" placeholder="请输入已付成本" />
          <span style="margin-left: 10px">{{ formatCurrency(this.form.projectPaidCost)}}</span>
        </el-form-item>
        <el-row >
          <el-col :span="12">
            <el-form-item label="logo" prop="projectLogo">
              <image-upload v-model="form.projectLogo"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="效果图" prop="projectImage">
              <image-upload v-model="form.projectImage"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProject, getProject, delProject,
  addProject, updateProject , getProjectRiskByProjectIdList} from "@/api/project/project";

export default {
  name: "Project",
  data() {
    return {
      // //el-badge给小红点计数用的
      // riskCount:0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目表格数据
      projectList: [],
      // 项目风险表格数据
      projectRiskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //风险信息弹窗
      RiskOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectName: null,
        projectPlan: null,
        projectActual: null,
        startDate: null,
        projectOpeningtime: null,
        projectCompletetime: null,
        projectTargetCost: null,
        projectPaidCost: null,
        projectLogo: null,
        projectImage: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        projectName: [
          { required: true, message: "项目名称不能为空", trigger: "blur" },
          { min: 1, max: 15, message: "项目名称长度必须介于 1 和 15 之间", trigger: "blur" }
        ],
        // 校验:projectPlan不能为空并且projectPlan是1-100的正整数
        projectPlan: [
          { required: true, message: "计划进度不能为空", trigger: "blur" },
          { pattern: /^[1-9]\d?$|^(100)$/, message: "计划进度必须为1-100的正整数!", trigger: "blur" }
        ],
        // 校验:projectActual不能为空并且projectActual是1-100的正整数
        projectActual: [
          { required: true, message: "实际进度不能为空", trigger: "blur" },
          { pattern: /^[1-9]\d?$|^(100)$/, message: "实际进度必须为1-100的正整数!", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    },
  filters:{
    numberFormat(value) {
      // 确保 value 是一个数字
      const number = parseFloat(value);
      // 使用 toLocaleString 方法格式化数字
      return number.toLocaleString('en-US', {
        minimumFractionDigits: 2, // 最小的小数位数
        maximumFractionDigits: 2  // 最大的小数位数
      });
    }
  },
  methods: {
    /** 价格单位处理 */
    formatCurrency(value) {
      const number = parseFloat(value);
      if (isNaN(number)) {
        return '非法输入';
      }
      let unit = '元';
      let scaledValue = number;
      if (scaledValue >= 100000000) { // 如果大于等于一亿
        scaledValue /= 100000000;
        unit = '亿';
      } else if (scaledValue >= 10000) { // 如果大于等于一万
        scaledValue /= 10000;
        unit = '万';
      }
      // 保留两位小数
      scaledValue = Math.round(scaledValue * 100) / 100;
      // 如果小于1，显示为约等于
      if (scaledValue < 1) {
        return `≈ ${scaledValue.toFixed(2)}${unit}`;
      }
      // 使用 toLocaleString 方法格式化数字
      return `${scaledValue.toLocaleString('en-US', {
        minimumFractionDigits: 2, // 最小的小数位数
        maximumFractionDigits: 2  // 最大的小数位数
      })}${unit}`;
    },
    /** 根据项目查看相应的项目风险 */
    handleRisk(projectId){
      this.RiskOpen = true;
      getProjectRiskByProjectIdList(projectId).then(response => {
        this.projectRiskList = response.data;
      }).catch(error => {});
    },
    /** 查询项目列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then(response => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        projectName: null,
        projectPlan: null,
        projectActual: null,
        startDate: null,
        projectOpeningtime: null,
        projectCompletetime: null,
        projectTargetCost: null,
        projectPaidCost: null,
        projectLogo: null,
        projectImage: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目";
    },
    /** 修改按钮操作 */
    handleUpdate(projectId) {
      this.reset();
      getProject(projectId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.projectId != null) {
            updateProject(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProject(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(projectId) {
      this.$modal.confirm('是否确认删除本项目的数据项？').then(function() {
        return delProject(projectId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
        // this.$modal.msgError("删除失败");
      });
    }
  }
};
</script>

<style>
.item {
  margin-top: 5px;
  margin-right: 40px;
}
</style>
